package com.elcl.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;

import com.elcl.storage.ApplicationCache;

/**
 * 获取资源的管理类(eg:获取asset的文件)
 * */
public class RecourceManagerUtil {

	/**
	 * @param recources
	 *            音乐在asset中的名称 播放asset中音乐文件
	 *            isInCall,是否为听筒模式
	 */
	public static MediaPlayer showPlayMusic(String recources,boolean isInCall) {
		AssetManager assetManager = ApplicationCache.context.getAssets();
		MediaPlayer mediaPlayer = new MediaPlayer();

		if (isInCall){
			AudioManager audioManager = (AudioManager)ApplicationCache.context
					.getSystemService(Context.AUDIO_SERVICE);
			audioManager.setMode(AudioManager.MODE_IN_CALL);
		}

		try {
			AssetFileDescriptor afd = assetManager.openFd(recources);
			mediaPlayer.setDataSource(afd.getFileDescriptor(),
					afd.getStartOffset(), afd.getLength());
			mediaPlayer.prepare();
			mediaPlayer.start();

			return mediaPlayer;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param recources
	 * @return 从asset中获取string类型数据
	 */
	public static String getAssetWithString(String recources) {
		AssetManager assetManager = ApplicationCache.context.getAssets();
		try {
			InputStreamReader inputReader = new InputStreamReader(
					assetManager.open(recources));
			BufferedReader bufReader = new BufferedReader(inputReader);
			String line = "";
			String Result = "";
			while ((line = bufReader.readLine()) != null)
				Result += line;
			return Result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
